
/**
 原生方法名常量
 */
const PRINT_CA = "PRINT_CA"  // 打印语句
const ALERT_SHOW_CA = "ALERT_SHOW_CA" // 弹出通用弹出框
const TOAST_SHOW_CA = "TOAST_SHOW_CA"  //弹出toast
const ADD_LISTENER = "ADD_LISTENER"  //添加回调函数
const CLOSE_CURRENT_PAGE_CA = "CLOSE_CURRENT_PAGE_CA" //关闭当前界面
const SHOW_NAV_CA = "SHOW_NAV_CA" //显示导航栏
const Hide_NAV_CA = "Hide_NAV_CA"           //隐藏导航栏
const GET_DEVICE_INFO_CA = "GET_DEVICE_INFO_CA"  //获取设备信息
const OPEN_ALBUM_CA = "OPEN_ALBUM_CA"             //打开相册
const OPEN_CAMERA_CA = "OPEN_CAMERA_CA"          //打开相机
const OPEN_VIDEO_CA = "OPEN_VIDEO_CA"          //拍摄视频
const PLAY_VIDEO_CA = "PLAY_VIDEO_CA"       //播放视频
const SCAN_QR_CODE_CA = "SCAN_QR_CODE_CA"  //扫码二维码
const OPEN_NATIVE_AC_CA = "OPEN_NATIVE_AC_CA" // 打开原生界面
const OPEN_STORE_PAGE_CA = "OPEN_STORE_PAGE_CA" //打开商店页面
const GET_NET_STATUS = "GET_NET_STATUS"   //获取网络状态
const CHECK_PERMISSION_CA = "CHECK_PERMISSION_CA"  //检查单个权限
const OPEN_PERMISSION_CA = "OPEN_PERMISSION_CA"   //开启单个权限
const OPEN_PERMISSIONS_CA = "OPEN_PERMISSIONS_CA"   //开启多个个权限
const GET_LOCATION_INFO_CA = "GET_LOCATION_INFO_CA"   //获取位置信息
const SAVE_CACHE_CA = "SAVE_CACHE_CA"    //保存数据
const READ_CACHE_CA = "READ_CACHE_CA"    //读取数据
const REMOVE_CACHE_CA = "REMOVE_CACHE_CA" //清理单个缓存
const PROCESSBAR_FULL_INIT_CA = "PROCESSBAR_FULL_INIT_CA"  //全屏进度条初始化
const PROCESSBAR_FULL_START_CA = "PROCESSBAR_FULL_START_CA"  //开始进度条
const PROCESSBAR_FULL_STOP_CA = "PROCESSBAR_FULL_Stop_CA" //结束进度条
const ACTION_SHEET_CA = "ACTION_SHEET_CA"   //action sheet
const ACTION_STATUSBAR_CA = "ACTION_STATUSBAR_CA"  //状态栏
const REQUEST_NET_CA = "REQUEST_NET_CA"         //网络请求

const DUN_INIT_CA = "DUN_INIT_CA"
const DUN_REGISTER_CA = "DUN_REGISTER_CA"
const DUN_ISREGISTER_CA = "DUN_ISREGISTER_CA"
const DUN_APPLY_CERT_CA = "DUN_APPLY_CERT_CA"
const DUN_P1_SING_CA = "DUN_P1_SING_CA"
const DUN_P7_SING_CA = "DUN_P7_SING_CA"
const DUN_CHANGE_PIN_CA = "DUN_CHANGE_PIN_CA"
const DUN_CHECK_PIN_CA = "DUN_CHECK_PIN_CA"
const DUN_GET_CERT_CA = "DUN_GET_CERT_CA"
const DUN_GET_CERT_INFO_CA = "DUN_GET_CERT_INFO_CA"
const DUN_ENC_CERT_ENC_CA = "DUN_ENC_CERT_ENC_CA"
const DUN_ENC_CERT_DEC_CA = "DUN_ENC_CERT_DEC_CA"
const DUN_SIGN_CERT_ENC_CA = "DUN_SIGN_CERT_ENC_CA"
const DUN_SIGN_CERT_DEC_CA = "DUN_SIGN_CERT_DEC_CA"

const OPEN_FACE_SERVICE_CA= "OPEN_FACE_SERVICE_CA"
const OPEN_OCR_SERVICE_CA= "OPEN_OCR_SERVICE_CA"   //打开ocr


//回调动作名称
const C_AlertCancel = "C_AlertCancel"  //alert 取消动作
const C_AlertSure = "C_AlertSure"      //alert 确定动作
const C_DEVICE_INFO = "C_DEVICE_INFO" //设备信息回调
const C_OPEN_ALBUM_CA = "C_OPEN_ALBUM_CA"  //打开相册回调
const C_OPEN_CAMERA_CA = "C_OPEN_CAMERA_CA"  //拍照回调
const C_OPEN_VIDEO_CA = "C_OPEN_VIDEO_CA"          //视频回调
const C_SCAN_QR_CODE_CA = "C_SCAN_QR_CODE_CA"    //扫码二维码回调
const C_OPEN_NATIVE_AC_CA = "C_OPEN_NATIVE_AC_CA" //打开原生界面
const C_GET_NET_STATUS = "C_GET_NET_STATUS"   //获取网络状态回调
const C_CHECK_PERMISSION_CA = "C_CHECK_PERMISSION_CA"  //检查单个权限回调
const C_OPEN_PERMISSION_CA = "C_OPEN_PERMISSION_CA"   //开启单个权限回调
const C_OPEN_PERMISSIONS_CA = "C_OPEN_PERMISSIONS_CA"   //开启多个权限回调
const C_GET_LOCATION_INFO_CA = "C_GET_LOCATION_INFO_CA"   //获取位置信息 回调
const C_SAVE_CACHE_CA = "C_SAVE_CACHE_CA"    //保存数据回调
const C_READ_CACHE_CA = "C_READ_CACHE_CA"    //读取数据回调
const C_REMOVE_CACHE_CA = "C_REMOVE_CACHE_CA" //清理单个缓存回调
const C_ACTION_SHEET_CA = "C_ACTION_SHEET_CA"   //action sheet 回调函数
const C_REQUEST_NET_CA = "C_REQUEST_NET_CA"         //网络请求回调

const C_DUN_INIT_CA = "C_DUN_INIT_CA"
const C_DUN_REGISTER_CA = "C_DUN_REGISTER_CA"
const C_DUN_ISREGISTER_CA = "C_DUN_ISREGISTER_CA"
const C_DUN_APPLY_CERT_CA = "C_DUN_APPLY_CERT_CA"
const C_DUN_P1_SING_CA = "C_DUN_P1_SING_CA"
const C_DUN_P7_SING_CA = "C_DUN_P7_SING_CA"
const C_DUN_CHANGE_PIN_CA = "C_DUN_CHANGE_PIN_CA"
const C_DUN_CHECK_PIN_CA = "C_DUN_CHECK_PIN_CA"
const C_DUN_GET_CERT_CA = "C_DUN_GET_CERT_CA"
const C_DUN_GET_CERT_INFO_CA = "C_DUN_GET_CERT_INFO_CA"
const C_DUN_ENC_CERT_ENC_CA = "C_DUN_ENC_CERT_ENC_CA"
const C_DUN_ENC_CERT_DEC_CA = "C_DUN_ENC_CERT_DEC_CA"
const C_DUN_SIGN_CERT_ENC_CA = "C_DUN_SIGN_CERT_ENC_CA"
const C_DUN_SIGN_CERT_DEC_CA = "C_DUN_SIGN_CERT_DEC_CA"

const C_OPEN_FACE_SERVICE_CA= "C_OPEN_FACE_SERVICE_CA"
const C_OPEN_OCR_SERVICE_CA= "C_OPEN_OCR_SERVICE_CA"
    

//MARK----样式
/**
 * 样式
 */
function CA_Style(){
    return{
        bgColor:undefined,     //背景颜色
        textColor:undefined,   //字体颜色
        textSize:undefined    //字体大小
    }
}
function PROCESSBAR_Style(){
    return{
        deaultColor:undefined ,   //正常颜色
         trackColor:undefined ,    //滑动颜色
         percentColor:undefined   //百分比颜色
    }
}
    
 //

function CaJssdk() {
    initSdk();
    return {
        toast:show_toast,   //显示toast
        print:print_ca,         //打印
        alert:show_alert ,       //显示alert
        closeWeb:closeCurrentPage , //关闭webview
        showNav:showCurrentNav,//显示导航
        hideNav:hideCurrenNav,   //隐藏导航栏
        getDeviceInfo:get_device_info, //获取设备信息
        openAlbum:openAlbum ,     //打开相册
        openCamera:openCamera ,  //拍照
        openVideo:openVideo,     //视频
        playVideo:playVideo, //播放视频
        scanQrCode:scanQrCode,  //扫码二维码
        openNativeActivity:openNativeActivity , //打开原生界面
        openStorePage:openStorePage, //打开指定界面
        getNetStatus:getNetStatus,   //打开网络状态
        checkPremission:checkPremission , //检查某个权限
        openPremission:openPremission , // 开启某个权限
        openPremissions:openPremissions, //开启多个权限
        getLocationInfo:getLocationInfo, //获取位置信息
        saveCache:saveCache, //保存数据
        readCache:readCache ,//读数据
        removeCache:removeCache , //清理某个缓存
        progressBar:{
            init:progressBarInit,   //初始化
            start:progressBarStart,  //设置进度条
            stop:progressBarStop     //关闭进度条
        } ,// 开启全局进度
        dun:{
            init:initDun,            //初始化手机盾
            isRegister:isRegisterDun, //是否注册
            register:registerDun,      //注册
            apply:applyCertDun,    //申请证书
            p1Sign:p1SignDun,   //p1签名
            p7Sign:p7SignDun,   //p7签名
            changePin:changePinDun,  //修改pin
            checkPin:checkPinDun,     //检查pin
            getCert:getCertDun,   //获取证书
            getCertInfo:getCertInfoDun, //获取证书内容
            encCertEnc:encryptionCertEncrytionDun,//加密证书加密
            encCertDec:encryptionCertDecryptDun, //加密证书解密
            signCertEnc:signatureCertEncrytionDun, //签名证书加密
            signCertDec:signatureCertDecryptDun,//签名证书解密
        },
        actionsheet:actionsheet,//actionsheet
        request:requestNet, //网络
        openOcr:openOcr, //打开ocr
        openFace:openFace , //打开人脸认证
    }
}
    

var caSdk = new CaJssdk();


/**
 初始化
*/
function initSdk(){
     console.log("initSdk:");
 
     sdkManage(ADD_LISTENER,'appCallBack')
}


/**
 原生回调js方法
 js 界面需要使用请调 调相应的动作方法，比如setAlertCancelCallback
 @param callResult js app回调 结果
 callResult={content:,callbackActiveName:}
 callBackCode: 发起时候code
 callbackActiveName: 回调动作名称
 content: 返回内容
 
*/
function appCallBack(callResult){

    
    
    let callResultJson =  JSON.parse(callResult)
    
    console.log('callbackActiveName'+callResultJson.callbackActiveName)
   
    
    if(callResultJson.callbackActiveName === C_AlertCancel ){
        alertCancelCallBack &&alertCancelCallBack(callResult)
     
    }else if(callResultJson.callbackActiveName === C_AlertSure){
        
        alertSureCallback && alertSureCallback(callResult)
    }else if (callResultJson.callbackActiveName === C_DEVICE_INFO){

          deviceInfoCallback(callResult)
    }else if(callResultJson.callbackActiveName === C_OPEN_ALBUM_CA){
        
          openAlbumCallback(callResult)
    }else if (callResultJson.callbackActiveName === C_OPEN_CAMERA_CA){
          openCameraCallback(callResult)
    }else if (callResultJson.callbackActiveName === C_SCAN_QR_CODE_CA){
          scanQrCodeCallBack(callResult)
    }else if(callResultJson.callbackActiveName === C_OPEN_NATIVE_AC_CA){
           openNativeAcCallback(callResult)
    }else if(callResultJson.callbackActiveName === C_GET_NET_STATUS){
           getNetStatusCallback(callResult)
    }else if(callResultJson.callbackActiveName === C_CHECK_PERMISSION_CA){
         checkPermissionCallback(callResult)
    }else if(callResultJson.callbackActiveName === C_OPEN_PERMISSION_CA){
         openPremissionCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_OPEN_PERMISSIONS_CA){
           openPermissionsCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_GET_LOCATION_INFO_CA){
        getLocationInfoCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_SAVE_CACHE_CA){
            saveCacheCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_READ_CACHE_CA){
            readCacheCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_REMOVE_CACHE_CA){
            removeCacheCallback(callResult)
    }else if (callResultJson.callbackActiveName ===C_DUN_INIT_CA){
          initDunCallback(callResult)
    }else if (callResultJson.callbackActiveName ===C_DUN_ISREGISTER_CA){
        isRegisterCallback(callResult)
    } else if(callResultJson.callbackActiveName ===C_DUN_REGISTER_CA){
        registerCallback(callResult)
    } else if(callResultJson.callbackActiveName ===C_DUN_APPLY_CERT_CA){
         applyCertCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_DUN_P1_SING_CA){
          p1SignCallback(callResult)
    }else if (callResultJson.callbackActiveName ===C_DUN_P7_SING_CA){
        p7SignCallback(callResult)
    }else if (callResultJson.callbackActiveName ===C_DUN_CHANGE_PIN_CA){
        changePinCallBack(callResult)
    }else if(callResultJson.callbackActiveName ===C_DUN_CHECK_PIN_CA){
        checkPinCallback(callResult)
    }else if (callResultJson.callbackActiveName ===C_DUN_GET_CERT_CA){
        getCertCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_DUN_GET_CERT_INFO_CA){
        getCertInfoCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_DUN_ENC_CERT_ENC_CA){
        encCerEncCallback(callResult)
    }else if (callResultJson.callbackActiveName ===C_DUN_ENC_CERT_DEC_CA){
        encCertDecCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_DUN_SIGN_CERT_ENC_CA){
        signCertEncCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_DUN_SIGN_CERT_DEC_CA){
        signCertDecCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_ACTION_SHEET_CA){
         actionsheetCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_REQUEST_NET_CA){
         requestCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_OPEN_OCR_SERVICE_CA){
            ocrCallback(callResult)
    }else if(callResultJson.callbackActiveName ===C_OPEN_FACE_SERVICE_CA){
           faceAuthCallback(callResult)
    }
  
    
}



/**
 sdk使用入口方法
 @params funcName android、ios端方法名
 @params paramsMap 参数格式 {code:200,body:content,message:"成功"}
       
         content为{}对象
 */
function sdkManage(funcName,paramsMap){
    if(isAndorid()){
              paramsMap? window.CAJSSDK[funcName](paramsMap): window.CAJSSDK[funcName]()
         
    }else if(isIos()){
          console.log("进来了--",funcName)


         window.webkit.messageHandlers[funcName].postMessage(paramsMap)
    }
}



/**
 原生打印语句
 */
  //--ok
function print_ca(message){
    console.log("打印方法")
    sdkManage(PRINT_CA,message)
}


/**
 *  弹出toast
 *  @param message:String toast内容
 */
 //--ok
function show_toast(message){
    console.log(message)
    sdkManage(TOAST_SHOW_CA,message)

}

//Mark:----alert

function CA_alert_param(){
     return{
         title:undefined,  //提示标题
         body:undefined,   //内容
         leftName:undefined,  //取消1
         rightName:undefined, //确定1
         singleName:undefined  //单个名字,如果存在，则只有一个按钮
         }
}
/**
 * 弹出alert
 *
 *  alertParms:CA_alert_param 枚举
 *  @param  sureCallback:function 确认回调方法
 *  @param cancelCallback:function  取消回调方法
 *
 * @return 取消 {"callbackActiveName":"C_AlertCancel","msg":"成功","result":true,"data":"弹出框取消回调结果"}
 * @return 确定 {"callbackActiveName":"C_AlertSure","result":true,"msg":"成功","data":"弹出框确定回调结果"}
 *
 */
  //--ok
 var alertSureCallback
 var alertCancelCallBack
function show_alert(alertParms,sureCallback,cancelCallback){

       alertSureCallback = sureCallback
      alertCancelCallBack =  cancelCallback
      
      let paramLast = {code:200,body:alertParms,message:"成功"}
    
      sdkManage(ALERT_SHOW_CA,JSON.stringify(paramLast))
}



/**
 * 关闭当前界面
 */
  //--ok
function closeCurrentPage(){
    sdkManage(CLOSE_CURRENT_PAGE_CA)
}

//Mark:----导航栏

/**
 * 导航栏参数
 */
  //--ok
function CA_nav_param(){

     return {
         title:undefined,  // 非必传 导航栏title
         style:undefined    // 非必传 导航栏样式
     }
}



/**
 * 显示导航栏
 * 参数格式
 *@param title 字符串
 *@param caStyle 样式 枚举CA_Style
 *
 */
  //--ok
function showCurrentNav(title,caStyle){


     var navParams = new CA_nav_param()
     
 
        navParams.title = title
        navParams.style = caStyle
     let paramLast = {code:200,body:navParams,message:"成功"}
      
    sdkManage(SHOW_NAV_CA,JSON.stringify(paramLast))
}

/**
 * 隐藏导航栏
 */
  //--ok
function hideCurrenNav(){
    
    sdkManage(Hide_NAV_CA)
}

//Mark:----设备信息
/**
 * 获取设备信息
 * @param callback  必传参数 回调方法
 * @return {"callbackActiveName":"C_DEVICE_INFO","msg":"成功","data":{"appVersion":"1.0","deviceToken":"337FEEDA-CD68-4B4E-BB0A-C12A9983E934","deviceModel":"iPhone 6","appDisplayName":"jssdk","systemVersion":"12.4.8"},"result":true}
 *
 * appVersion 版本号
 * deviceToken  设备唯一码
 * appDisplayName app名称
 * systemVersion 系统版本
 * deviceModel 设备信号
 */
  //--ok
var deviceInfoCallback //回调函数
function get_device_info(callback){
    deviceInfoCallback = callback
    sdkManage(GET_DEVICE_INFO_CA)
}

//Mark:----相册、相机相关
/**
 * 相册
 * @return 成功 {"msg":"用户取消了","result":true,"data":{"base64":'/9j',filePath:''},"callbackActiveName":"C_OPEN_ALBUM_CA"}
 * @return 失败 {"msg":"用户取消了","result":false,"data":"","callbackActiveName":"C_OPEN_ALBUM_CA"}
 */
  //--ok
var openAlbumCallback;// 打开相册回调
function openAlbum(callback){
    openAlbumCallback =  callback
    sdkManage(OPEN_ALBUM_CA)
}

/**
 * 拍照
 * @return 成功 {"msg":"用户取消了","result":true,"data":{"base64":'',filePath:''},"callbackActiveName":"C_OPEN_ALBUM_CA"}
 * @return 失败 {"msg":"用户取消了","result":false,"data":"","callbackActiveName":"C_OPEN_ALBUM_CA"}
 */
  //--ok
var openCameraCallback; //打开相机拍照回调
function openCamera(callback){
    openCameraCallback =  callback
    sdkManage(OPEN_CAMERA_CA)
}

/**
 * 拍摄视频  （视频只需要返回filePath，不需要返回base64）
 * @return 成功 {"data":{"filePath":"file:"},"msg":"成功","result":true,"callbackActiveName":"C_OPEN_VIDEO_CA"}
 * @return 失败 {"callbackActiveName":"C_OPEN_ALBUM_CA","data":"","result":false,"msg":"用户取消了!"}
 *
 */
  //--ok
var openVideoCallback; //拍摄视频
function openVideo(callback){
    openVideoCallback =  callback
    sdkManage(OPEN_VIDEO_CA)
}

/**
 * 打开视频
 * @param  videoUrl 视频路径
 */
  //--ok
function playVideo(videoUrl){
     let paramLast = {code:200,body:videoUrl,message:"成功"}
    sdkManage(PLAY_VIDEO_CA,JSON.stringify(paramLast))
}

/**
 * 扫码二维码
 * @return  {"result":true,"msg":"成功","callbackActiveName":"C_SCAN_QR_CODE_CA","data":"http://---"}
 */
  //--ok
 var scanQrCodeCallBack;
function scanQrCode(callback){
    scanQrCodeCallBack = callback
    sdkManage(SCAN_QR_CODE_CA)
}

//MARK:----openNativeActivity

/**
 * 打开原生界面
 * @param  acName 界面名称
 * @return 成功 {"callbackActiveName":"C_OPEN_NATIVE_AC_CA","msg":"成功","data":"打开成功","result":true}
 * @return 失败 {"callbackActiveName":"C_OPEN_NATIVE_AC_CA","result":false,"data":"","msg":"找不到界面"}
 */
  //--ok
 var openNativeAcCallback;
function openNativeActivity(acName,callback){
    openNativeAcCallback = callback
      let paramLast = {code:200,body:acName,message:"成功"}
      
    sdkManage(OPEN_NATIVE_AC_CA,JSON.stringify(paramLast))
}

/**
 * 打开商店指定页面
 * @param  storeUrl 商店地址
 */
 //--ok
function openStorePage(storeUrl){
     let paramLast = {code:200,body:storeUrl,message:"成功"}
    sdkManage(OPEN_STORE_PAGE_CA,JSON.stringify(paramLast))
}

//MARK: ----获取网络状态
/**
 * 获取网络状态
 * @return  {"msg":"成功","data":"WIFI","result":true,"callbackActiveName":"C_GET_NET_STATUS"}
 *
 * 参数说明data：
 * WIFI wifi网络返回
 * WAN 手机网络返回
 * NONET 没有连接网络
 */
 //--ok
var getNetStatusCallback
function getNetStatus(callBack){
    getNetStatusCallback = callBack
    sdkManage(GET_NET_STATUS)
}

//MARK:---android权限

var ANDROID_PERMISSIONS ={
    READ_EXTERNAL_STORAGE:1,
    WRITE_EXTERNAL_STORAGE:2,
    CAMERA:3,
    READ_PHONE_STATE:4,
}

/**
 * 检查单个权限
 * @param permissionName 权限名称，传ANDROID_PERMISSIONS 对应值
 * @param  callBack 回调
 * @return {"callbackActiveName":"C_CHECK_PERMISSION_CA","data":"","msg":"失败","result":false}"
 * @return {"callbackActiveName":"C_CHECK_PERMISSION_CA","data":"2","msg":"成功","result":true}"
 * 参数说明 data:权限对应值    result :是否有权限
 */
 var checkPermissionCallback
function checkPremission(permissionName,callBack){
    checkPermissionCallback = callBack
    let paramLast = {code:200,body:permissionName,message:"成功"}
    sdkManage(CHECK_PERMISSION_CA,JSON.stringify(paramLast))
}

/**
 * 开启单个权限
 * @param permissionName 权限名称，传ANDROID_PERMISSIONS 对应值
 * @param  callBack 回调
 * @return  {"callbackActiveName":"C_OPEN_PERMISSION_CA","data":"","msg":"失败","result":false}"
  *  @return    {"callbackActiveName":"C_OPEN_PERMISSION_CA","data":"2","msg":"成功","result":true}"
 * 参数说明data:成功会返回成功的权限的对应值  result:是否成功
 */
var openPremissionCallback
function openPremission(permissionName,callBack){
    openPremissionCallback =  callBack
    let paramLast = {code:200,body:permissionName,message:"成功"}
    sdkManage(OPEN_PERMISSION_CA,JSON.stringify(paramLast))
}

/**
 * 开启多个权限
 * * @param permissionNames 数组 权限名称，传 [ANDROID_PERMISSIONS] 对应值
 * @param  callBack 回调
 * @return  {"callbackActiveName":"C_OPEN_PERMISSION_CA","data":"","msg":"失败","result":false}"
  *  @return    {"callbackActiveName":"C_OPEN_PERMISSIONS_CA","data":"[2]","msg":"成功","result":true}"
  *  @return    {"callbackActiveName":"C_OPEN_PERMISSIONS_CA","data":"[2,3]","msg":"成功","result":true}"
  *    参数说明 data:成功会返回成功的权限的对应值  result:只有全部权限一个没拿到才是false，只要拿到了一个权限就是true，拿到的权限在data里面
 */
 var openPermissionsCallback
function openPremissions(permissionNames,callBack){
    openPermissionsCallback = callBack
    let paramLast = {code:200,body:permissionNames.toString(),message:"成功"}
    sdkManage(OPEN_PERMISSIONS_CA,JSON.stringify(paramLast))
}

//MARK ---位置
/**
 * 获取位置信息
 * @return {"data":{"name":"中海国际中心E座","longitude":104.05295457921407,"locality":"成都市","thoroughfare":"交子大道333号","administrativeArea":"四川省","latitude":30.584140859566617,"subLocality":"武侯区"},"callbackActiveName":"C_GET_LOCATION_INFO_CA","msg":"成功","result":true}
 *
 * data参数说明 ：
 * latitude：经度
 * longitude：纬度
 * administrativeArea：省
 * locality：市
 * subLocality：区
 * thoroughfare：街道
 * name：具体地址
 */
 //--ok
 var getLocationInfoCallback
function getLocationInfo(callBack){
    getLocationInfoCallback = callBack
    sdkManage(GET_LOCATION_INFO_CA)
}

//MARK:---数据存储

/**
 * 存储数据
 * @param key 存储数据的key
 * @param value 存储的数据 可以是字符串也可以是map
 * @param callBack 回调函数
 * @return 成功 {"result":true,"data":true,"callbackActiveName":"C_SAVE_CACHE_CA","msg":"成功"}
 * @return 失败 {"result":false,"data":"存储失败","callbackActiveName":"C_SAVE_CACHE_CA","msg":"失败"}
 */
 //---ok
var saveCacheCallback
function saveCache(key,value,callBack){
    saveCacheCallback = callBack

    let paramMap = {key:key,value:value}
    let paramLast = {code:200,body:paramMap,message:"成功"}
    sdkManage(SAVE_CACHE_CA,JSON.stringify(paramLast))
}


/**
 * 读取数据
 * @param key 数据的key（存储时候的key）
 * @param callBack 回调函数
 * @return 成功  {"data":{},"msg":"成功","callbackActiveName":"C_READ_CACHE_CA","result":true}
 * data参数说明 ：
 * data：可以为字符串，也可以为{}对象
 * @return 失败 {"data":"","result":false,"callbackActiveName":"C_READ_CACHE_CA","msg":"读取失败"}
 */
 //---ok
var readCacheCallback
function readCache(key,callBack){
    readCacheCallback = callBack
     let paramLast = {code:200,body:{key:key},message:"成功"}
    sdkManage(READ_CACHE_CA,JSON.stringify(paramLast))
}

/**
 * 根据key清理单个缓存
 * @param key 数据的key
 * @param callBack 回调函数
 * @return 成功 {"data":"成功","callbackActiveName":"C_REMOVE_CACHE_CA","msg":"成功","result":true}
 * @return 失败 {"data":"","callbackActiveName":"C_REMOVE_CACHE_CA","msg":"失败,缓存不存在","result":false}
 */

 //--ok
 var removeCacheCallback
function removeCache(key,callBack){
    removeCacheCallback = callBack
    let paramLast = {code:200,body:{key:key},message:"成功"}
    sdkManage(REMOVE_CACHE_CA,JSON.stringify(paramLast))
}

//MARK:---
/**
 * 初始化进度条
 * @param bar_style 进度条样式 PROCESSBAR_Style
 */
 //--ok
 
function progressBarInit(bar_style){


    let paramLast = {code:200,body:bar_style,message:"成功"}
       console.log("尽力了---",paramLast)
    sdkManage(PROCESSBAR_FULL_INIT_CA,JSON.stringify(paramLast))
}

/**
 * 进度条进度
 * @param  percentage 进度条进度
 */
  //--ok
function progressBarStart(percentage){
    
    let paramLast = {code:200,body:percentage,message:"成功"}
    sdkManage(PROCESSBAR_FULL_START_CA,JSON.stringify(paramLast))
}

/**
 * 关闭进度条
 *
 */
  //--ok
function progressBarStop(){
      console.log("关闭进度条---")
    sdkManage(PROCESSBAR_FULL_STOP_CA)
}


//MARK:-----手机盾

/**
 * 初始化手机盾
 * @param username 用户名
 * @param callBack 回调函数
 * @return 成功
 * {"data":{"result":"0X00000000","msg":"ok"},"result":true,"msg":"成功","callbackActiveName":"DUN_INIT_CA"}
 * @return 失败
 * {"msg":"请先init手机盾","data":"","callbackActiveName":"DUN_INIT_CA","result":false}
 */

 //---ok
 var initDunCallback
function initDun(username,callBack){

     initDunCallback =  callBack
    let paramLast = {code:200,body:username,message:"成功"}


   sdkManage(DUN_INIT_CA,JSON.stringify(paramLast))
}

/**
 * 是否注册
 * @param callBack 回调函数
 * @return 成功
 * {"msg":"成功","result":true,"data":{"result":"0X00000000","msg":"用户已存在"},"callbackActiveName":"C_DUN_ISREGISTER_CA"}
 * @return 失败
 * {"msg":"失败","result":false,"data":"用户不存在","callbackActiveName":"C_DUN_ISREGISTER_CA"}
 */

 //--ok
var isRegisterCallback
function isRegisterDun(callBack){
console.log("--ddd---")
   isRegisterCallback = callBack
   let paramLast = {code:200,body:username,message:"成功"}
   sdkManage(DUN_ISREGISTER_CA,JSON.stringify(paramLast))
}

/**
 * 用户注册
 * @param callBack 回调函数
 * @return 成功
 * {"callbackActiveName":"C_DUN_REGISTER_CA","data":{"result":"0X00000000","msg":"注册成功"},"msg":"成功","result":true}
 *
 * @return 失败
 * {"data":"","msg":"失败","callbackActiveName":"C_DUN_REGISTER_CA","result":false}
 */
  //---ok
var registerCallback
function registerDun(callBack){
    registerCallback = callBack
    sdkManage(DUN_REGISTER_CA)

}

/**
 * 申请证书
 * @param pin  用户证书pin码
 * @param  callBack 回调函数
 *
 * @return 成功
 * {"msg":"成功","result":true,"callbackActiveName":"C_DUN_APPLY_CERT_CA","data":{"result":"0X00000000","msg":"更新数字证书成功","command":"updateCert"}}
 * @return 失败
 * {"data":"","callbackActiveName":"C_DUN_APPLY_CERT_CA","msg":"请先init手机盾","result":false}
 */
  //---ok
var  applyCertCallback
function applyCertDun(pin,callBack){
    applyCertCallback  =  callBack
     let paramLast = {code:200,body:pin,message:"成功"}
     sdkManage(DUN_APPLY_CERT_CA,JSON.stringify(paramLast))
}


/**
 * p1 签名
 * @param signOri 签名原文
 * @param pin    手机盾pin码
 * @param isBase64  原文是否是base64
 * @param callBack  回调函数
 * @return 成功
 * {"result":true,"msg":"成功","data":{"result":"0X00000000","sign":"MEQCIBDVlfBpQ9MApfxWMUjdULL\/siSNns6gMv1Q9EpkApFxAiBiL\/eJ0XAj17XTixa+5FgUel5\/xxnvtd3QPDGK49YX5Q=="},"callbackActiveName":"C_DUN_P1_SING_CA"}
 * @return 失败
 * {"data":"","msg":"请先init手机盾","callbackActiveName":"C_DUN_P1_SING_CA","result":false}
 *
 */
  //---ok
 var p1SignCallback
function p1SignDun(signOri,pin,isBase64,callBack){
    p1SignCallback = callBack
   let params = {signOri:signOri,pin:pin,isBase64:isBase64}
   let paramLast = {code:200,body:params,message:"成功"}

   sdkManage(DUN_P1_SING_CA,JSON.stringify(paramLast))
    
}

/**
 * P7签名
 * @param signOri 签名原文
 * @param pin    手机盾pin码
 * @param callBack  回调函数
 * @return 成功
 * {"data":{"result":"0X00000000","P7Sign":"MII...JpVw=="},"result":true,"callbackActiveName":"C_DUN_P7_SING_CA","msg":"成功"}
 * @return 失败
 * {"callbackActiveName":"C_DUN_P7_SING_CA","result":false,"data":"","msg":"请先init手机盾"}
 */
  //---ok
var p7SignCallback
function p7SignDun(signOri,pin,callBack){
    p7SignCallback = callBack
      let params = {signOri:signOri,pin:pin}
   let paramLast = {code:200,body:params,message:"成功"}
    sdkManage(DUN_P7_SING_CA,JSON.stringify(paramLast))
}


/**
 * 修改pin码
 * @param oldPin 旧pin
 * @param newPin
 * @param callBack  回调函数
 * @retrun 成功
 * {"callbackActiveName":"C_DUN_CHANGE_PIN_CA","msg":"成功","result":true,"data":{"result":"0X00000000","msg":"ChangePin Success"}}
 * @return 失败
 * {"msg":"用户名不存在","data":"","result":false,"callbackActiveName":"C_DUN_CHANGE_PIN_CA"}
 */
  //---ok
 var changePinCallBack
function changePinDun(oldPin,newPin,callBack){
    changePinCallBack = callBack
     let params = {oldPin:oldPin,newPin:newPin}
     let paramLast = {code:200,body:params,message:"成功"}
     sdkManage(DUN_CHANGE_PIN_CA,JSON.stringify(paramLast))
}

/**
 * 检查pin码
 * @param oldPin pin码
 * @param callBack  回调函数
 * @return 成功
 * {"result":true,"data":{"result":"0X00000000","msg":"ok"},"callbackActiveName":"C_DUN_INIT_CA","msg":"成功"}
 * @return 失败
 * {"msg":"请先init手机盾","callbackActiveName":"C_DUN_CHECK_PIN_CA","result":false,"data":""}
 *
 */
  //---ok
var checkPinCallback
function checkPinDun(oldPin,callBack){
    checkPinCallback = callBack
      let params = {oldPin:oldPin}
     let paramLast = {code:200,body:params,message:"成功"}
     sdkManage(DUN_CHECK_PIN_CA,JSON.stringify(paramLast))
}

/**
 * 获取证书信息
 * @param callBack 回调函数
 * @return {"msg":"成功","data":{"result":"0X00000000","info":{"encCert":"MII..IT0LtDw==","encPrivate":"userSeal=MHkC...her=SM4","signCert":"MIIDV...Yg=="},"msg":"获取证书成功","resultcode":"0","command":"getCert","errCode":"0X01000000"},"result":true,"callbackActiveName":"C_DUN_GET_CERT_CA"}
 * @return 失败
 * {"msg":"请先init手机盾","data":"","result":false,"callbackActiveName":"C_DUN_GET_CERT_CA"}
 *
 */
  //---ok
 var getCertCallback
function getCertDun(callBack){
    getCertCallback = callBack
    sdkManage(DUN_GET_CERT_CA)
}

/**
 * 获取证书详情信息
 * @param callBack 回调函数
 * @return 成功
 * {"result":true,"data":{"result":"0X00000000","cert":{"from":"2021-09-16 16:55:16","issuer":"C=CN,O=SCCATESTSM2,OU=TESTSM2,CN=SCEGBTESTSM2","country":"CN","pubKey":"SM2-256","signAlg":"SM2","hashAlg":"SM3WithSM2Encryption","seriealNum":"616d73deaea0ec7826d497185404a2408415bbf8","version":"3","to":"2021-10-01 16:55:16","user":"C=CN,O=SCCATESTSM2,OU=SCEGBTESTSM2,OU=PN:3,OU=ID3:2,OU=NO.2:1,OU=NO.1:4,SURNAME=510321,CN=ffy"},"msg":"证书获取成功","resultcode":"0","command":"certinfo","errCode":"0X01000000"},"callbackActiveName":"C_DUN_GET_CERT_INFO_CA","msg":"成功"}
 *
 * @return 失败
 * {"data":"","msg":"请先init手机盾","result":false,"callbackActiveName":"C_DUN_GET_CERT_INFO_CA"}
 */
  //---ok
var getCertInfoCallback
function getCertInfoDun(callBack){
    getCertInfoCallback = callBack
    sdkManage(DUN_GET_CERT_INFO_CA)
}


/**
 * 加密证书加密
 * @param encryptionOri 加密原文 非base64
 * @param callBack 回调函数
 * @return 成功
 * {"msg":"成功","callbackActiveName":"C_DUN_ENC_CERT_ENC_CA","data":{"result":"0X00000000","ciphertext":"uQAAeSXtZ9WTJSrvArotrYUkaeKCIj0PAhR7bLuwO2JMWv6M7b7x6LasjtXLQeRPjtFMoo+ebnEw+N8Blqwx79wULcNC3oNg+3btREITGQsQBRrZsyHRfZ48b34fRgk\/nQFFupNCyGE="},"result":true}

 * @return 失败
 * {"data":"","msg":"请先init手机盾","callbackActiveName":"C_DUN_ENC_CERT_ENC_CA","result":false}
 */
  //---ok
 var encCerEncCallback
function encryptionCertEncrytionDun(encryptionOri,callBack){
    encCerEncCallback = callBack
     let paramLast = {code:200,body:encryptionOri,message:"成功"}
     sdkManage(DUN_ENC_CERT_ENC_CA,JSON.stringify(paramLast))
}

/**
 * 加密证书解密
 * @param encResult 加密证书加密结果
 * @param pin pin码
 * @return 成功
 * {"result":true,"data":{"plain":"mytest","result":"0X00000000"},"callbackActiveName":"C_DUN_ENC_CERT_DEC_CA","msg":"成功"}
 * @return 失败
 * {"result":false,"msg":"解密内容不能为空","data":"","callbackActiveName":"C_DUN_ENC_CERT_DEC_CA"}
 */
  //---ok
var encCertDecCallback
function encryptionCertDecryptDun(encResult,pin,callBack){
    encCertDecCallback = callBack
     let params = {encResult:encResult,pin:pin}
     let paramLast = {code:200,body:params,message:"成功"}
     sdkManage(DUN_ENC_CERT_DEC_CA,JSON.stringify(paramLast))
}
 //---ok
/**
 * 签名证书加密
 * @param signOri 原文
 * @param callBack 回调函数
 * @return 成功
 * {"result":true,"callbackActiveName":"C_DUN_SIGN_CERT_ENC_CA","msg":"成功","data":{"result":"0X00000000","ciphertext":"pp6mGQ5+feM3R2xaRDtfryJRZnKX4v6dJwGiktTCI3H1uP4QfM+f+7rsCa5QDIpVauE0FruFreKO8fSESYTkXBL7vGLiDZVe2rQI2\/2PrGNNRrXZraoI3HO1U9XcckHm6ce80cKukg=="}}
 * @return 失败
 * {"result":false,"data":"","msg":"请先init手机盾","callbackActiveName":"C_DUN_SIGN_CERT_ENC_CA"}
 */
  //---ok
var signCertEncCallback
function signatureCertEncrytionDun(signOri,callBack){
    signCertEncCallback = callBack
    console.log("signatureCertEncrytionDun")
     let paramLast = {code:200,body:signOri,message:"成功"}
     sdkManage(DUN_SIGN_CERT_ENC_CA,JSON.stringify(paramLast))
}

/**
 * 签名证书解密
 * @param signResult  签名结果
 * @param pin   pin码
 * @param  callBack   回调函数
 * @return 成功
 * {"data":{"result":"0X00000000","plain":"signori"},"callbackActiveName":"C_DUN_SIGN_CERT_DEC_CA","msg":"成功","result":true}
 * @return 失败
 * {"result":false,"callbackActiveName":"C_DUN_ENC_CERT_DEC_CA","msg":"解密内容不能为空","data":""}
 */

 //---ok
 var signCertDecCallback
function signatureCertDecryptDun(signResult,pin,callBack){
    signCertDecCallback = callBack
     let params = {signResult:signResult,pin:pin}
     let paramLast = {code:200,body:params,message:"成功"}
      sdkManage(DUN_SIGN_CERT_DEC_CA,JSON.stringify(paramLast))
}


/**
 * actionsheet
 * @param title 标题
 * @param content 内容
 * @param callBack 回调函数
 * @return 成功
 * {"callbackActiveName":"C_ACTION_SHEET_CA","data":"001","msg":"成功","result":true}
 *
 * @return 取消
 * {"msg":"取消","data":"","callbackActiveName":"C_ACTION_SHEET_CA","result":false}
 */
var actionsheetCallback
function actionsheet(title,content,callBack){

    let params = {title:title,content:content}
    actionsheetCallback = callBack
     let paramLast = {code:200,body:params,message:"成功"}
      sdkManage(ACTION_SHEET_CA,JSON.stringify(paramLast))
     
}
/**
 *  @param url 请求地址
 *  @param params 请求参数，参数为键值格式{name:"zs"}
 *  @param method 参考枚举method_request
 *  @param headers 请求头 非传参数，如果不传则默认不是application/json 格式  {"Content-Type":"application/json;charset=UTF-8"}
 *  @param callBack 回调函数
 *  @return 成功
 *  {"result":true,"callbackActiveName":"C_REQUEST_NET_CA","msg":"成功","data":{"userName":"成都","age":"f51d665d9242ce03a07dde11280ae133"}}
 *  @return 失败
 *  {"msg":"连接服务器错误","callbackActiveName":"C_REQUEST_NET_CA","result":false,"data":""}
 *
 */
var requestCallback
var method_request = {POST:"POST",GET:"GET",UPLOAD:"UPLOAD"}
function requestNet(url,params,method,headers,callBack){
    
    requestCallback = callBack
    console.log("----callBack",callBack)
    let params2 = {url:url,params:params,method:method,headers:headers}

      let paramLast = {code:200,body:params2,message:"成功"}
    sdkManage(REQUEST_NET_CA,JSON.stringify(paramLast))
}



function OCR_CARDTYPE(){
    return{
       front:'front',
       back:'back'
    }
}
/**
 * @param cardType 枚举（OCR_CARDTYPE） 身份证正反面 front 正面 back 背面
 *  @return
 * 正面
 *  {"data":{"sign":"PLEASE_REFRESH_YOUR_TICKET_NOW",
 *  "sex":"男","birth":"","idcard":"362330------X","nation":"汉","orderNo":"orderNO4538644049594","address":"江西省--号",
 *  "frontMultiWarning":"00010001",
 *  "frontCrop":"\/9j\/4=","frontClarity":"98",
 *  "frontWarning":"00010000",
 *  "name":""},
 *  "result":true,"callbackActiveName":"C_OPEN_OCR_SERVICE_CA","msg":"成功"}
 *
 *  背面
 *
 *  {"msg":"成功","callbackActiveName":"C_OPEN_OCR_SERVICE_CA","result":true,"data":{"backWarning":"00000001","validDate":"20171023-20371023","backClarity":"   27","backMultiWarning":"00000001","authority":"鄱阳县公安局","orderNo":"orderNO6550229660369","sign":"PLEASE_REFRESH_YOUR_TICKET_NOW","backCrop":"\/9j\/4"}   }
 *  @return 失败
 * {"msg":"用户取消操作","result":false,"data":"","callbackActiveName":"C_OPEN_OCR_SERVICE_CA"}
 */

var ocrCallback;
function openOcr(cardType,callBack){
     ocrCallback = callBack
     let params = {cardType:cardType}
     let paramLast = {code:200,body:params,message:"成功"}
    
    sdkManage(OPEN_OCR_SERVICE_CA,JSON.stringify(paramLast))
}

/**
 * @param  cardId 身份证号码
 * @param  name   姓名
 *  @return 成功
 *  {"msg":"成功","callbackActiveName":"C_OPEN_FACE_SERVICE_CA","result":true,"data":"认证成功"}
 *  @return 失败
 * {"msg":"用户取消","data":"","callbackActiveName":"C_OPEN_OCR_SERVICE_CA","result":false}
 */
 var faceAuthCallback
function openFace(cardId,name,callBack){
    faceAuthCallback =  callBack
    let params={cardId:cardId,name:name}
    let baseParams={code:200,body:params,message:"成功"}
    sdkManage(OPEN_FACE_SERVICE_CA,JSON.stringify(baseParams))
}



function isAndorid(){
    var ret = false;
    var u = navigator.userAgent;
    if (u.indexOf("Android") > -1 || u.indexOf("Adr") > -1) {

        ret = true;
    }
    return ret;
}

function isIos(){
    var ret = false;
    var u = navigator.userAgent;
    //if (/ios|iphone|ipod|pad/i.test(u)) {
    if (u.indexOf("iPhone") > -1 || u.indexOf("iOS") > -1) {
        ret = true;
    }
    return ret;

}

